package org.jod.anarchytrek.ship;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.jod.anarchytrek.componentfactory.AComponentFactory;
import org.jod.anarchytrek.util.StructuralUnitsUtil;

public abstract class ASchip {
    
    private int _structuralUnits;
    private int _shipClass;
    private Map _buildableComponents;

    public ASchip(int aShipClass) {
        
        _structuralUnits = StructuralUnitsUtil.getByClassShip(aShipClass);
        _shipClass = aShipClass;
        _buildableComponents = new HashMap();
    }

    
    public int shipClass() {
        return _shipClass;
    }
    
    public int structuralUnits() {
        return _structuralUnits;
    }
    
    public void addComponent(String aComponentType, AComponentFactory aFactory) {
        _buildableComponents.put(aComponentType, aFactory);
    }
    
    public void removeComponent(String aComponentType) {
        _buildableComponents.remove( aComponentType);
    }
    
    public void build() {
        
        Collection buildableComponentsCollection =  _buildableComponents.values();
        Iterator buildableComponentsIterator =  buildableComponentsCollection.iterator();

        while (buildableComponentsIterator.hasNext()) {
            forEach( (AComponentFactory)buildableComponentsIterator.next()  );
        }
        
    }

    private void forEach(AComponentFactory aFactory) {
        aFactory.build();
    }

    public  abstract void register();
}
